public class Tree {
    static class TreeNode {
        public char val;
        public Tree.TreeNode left;
        public Tree.TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }

    public Tree.TreeNode creteTree() {
        Tree.TreeNode A = new Tree.TreeNode('A');
        Tree.TreeNode B = new Tree.TreeNode('B');
        Tree.TreeNode C = new Tree.TreeNode('C');
        Tree.TreeNode D = new Tree.TreeNode('D');
        Tree.TreeNode E = new Tree.TreeNode('E');
        Tree.TreeNode F = new Tree.TreeNode('F');
        Tree.TreeNode G = new Tree.TreeNode('G');
        Tree.TreeNode H = new Tree.TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        //E.right = H;
        C.left = F;
        C.right = G;
        return A;//根节点
    }
    //前序遍历
    public void preOrder(Tree.TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    void inOrder(Tree.TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后序遍历
    void postOrder(Tree.TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    //获取树中节点的个数
    public static int count = 0;
    public void size(Tree.TreeNode root){
        if(root == null) {
            return ;
        }
        count++;
        size(root.right);
        size(root.left);
    }
    //获取叶子节点的个数
    public static int leafCount = 0;
    void getLeafNodeCount1(Tree.TreeNode root){
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }
    // 获取第K层节点的个数
    int getKLevelNodeCount(Tree.TreeNode root, int k){
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)
                + getKLevelNodeCount(root.right,k-1);
    }
    //查找节点
    Tree.TreeNode find(Tree.TreeNode root, int val){
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        Tree.TreeNode ret = find(root.left,val);
        if(ret != null) {
            return ret;
        }
        ret = find(root.right,val);
        if(ret != null) {
            return ret;
        }
        return null;
    }
}
